define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    $.widget('rk.colleagueContentFollowMe', {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _init:function(){
            var me = this;
            me.loaddata({pageNo:1,pageSize:20});
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            elem.on("click", "[act='colleague_content_follow_button']", function(){
                var follow = $(this);
                var uid = follow.attr("uid");
                var followStatus = follow.attr("follow");
                if(follow.hasClass('disable')) return;
                follow.addClass('disable');
                if(followStatus == 1){
                    rk.post("/json/oa_relation/delFollow.action",{fId:uid},function(json){
                        if(json.status == 0) {
                            follow.attr("follow", "0");
                            follow.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                            follow.removeClass("has_follow");
                            follow.addClass("no_follow");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        follow.removeClass('disable');
                    });
                }else if(followStatus == 0){
                    rk.post("/json/oa_relation/addFollow.action",{fId:uid},function(json){
                        if(json.status == 0) {
                            follow.attr("follow", "1");
                            follow.attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            follow.removeClass("no_follow");
                            follow.addClass("has_follow");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        follow.removeClass('disable');
                    });
                }
            });
        },
        loaddata:function(param){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if(!param){
                param = {pageNo:1,pageSize:20};
            }
            param.lookedUser = opt.uid;
            rk.post("/json/oa_relation/followeds.action",param,function(json){
                if(json.status == 0) {
                    var temp = require("oa/tmpl/colleague/content/tpl_colleague_content_follow_me.tpl");
                    var followedUsers = json.followedUsers;
                    if(followedUsers && followedUsers.length > 0){
                        $.each(followedUsers, function (i, dataJson) {
                            if(!dataJson.smallIcon){
                                dataJson.smallIcon = rk.DEFAULT_USER_ICON;
                                followedUsers[i] = dataJson;
                            }
                        });
                    }
                    json.followedUsers = followedUsers;
                    var html = rk.templateText(temp, json);
                    elem.html(html);
                    //colleague_content_follow_me_page
                    elem.find('[act="colleague_content_follow_me_page"]').rkPaging({
                        pagenum: json.pageNo,  //（可选）当前页数，默认1
                        pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                        rowscount: json.dataCount, //所有数据数
                        onChange: function (pagenum) {
                            var params = param;
                            params.pageNo = pagenum;
                            params.pageSize = json.pageSize;
                            me.loaddata(params);
                        }
                    });
                }else{
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
                elem.find("[act='colleague_content_follow_button']").beTip({dir:"left"});
            });
        }
    });
});